package com.ruoyi.web.controller.business.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.goods.domain.JbProduct;
import com.ruoyi.business.goods.domain.JbProductClassify;
import com.ruoyi.business.goods.service.JbProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.business.goods.factory.JbProductClassifyFactory;
import com.ruoyi.web.controller.business.goods.factory.JbProductFactory;
import com.ruoyi.web.controller.system.factory.SysDataScopeFactory;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 产品 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("产品")
@RestController
@RequestMapping("/product")
public class JbProductController extends JoController
{
    @Autowired
    private JbProductService service;
    @Autowired
    private JbProductFactory factory;
    @Autowired
    private JbProductClassifyFactory jbProductClassifyFactory;
    @Autowired
    private SysDataScopeFactory sysDataScopeFactory;

    /**
     * 查询产品列表
     */
    @ApiOperation("查询产品列表")
    @GetMapping("/list")
    public TableDataInfo page(PageParam page, JbProduct jbProduct)
    {
        IPage<JbProduct> iPage = service.page(page.b(), factory.buildQW(page, jbProduct));
        return JoR.rows(iPage);
    }

    /**
     * 查询全部
     */
    @ApiOperation("查询全部")
    @GetMapping("/listAll")
    public AjaxResult listAll(PageParam page, JbProduct jbProduct)
    {
        return AjaxResult.success(service.list(factory.buildQW(page, jbProduct)));
    }

    /**
     * 导出产品列表
     */
    @ApiOperation("导出产品列表")
    @PreAuthorize("@ss.hasPermi('product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbProduct jbProduct)
    {
        LambdaQueryWrapper<JbProduct> select = new LambdaQueryWrapper<>();
        IPage<JbProduct> iPage = service.page(page.b(), select);

        List<JbProduct> list = iPage.getRecords();
        ExcelUtil<JbProduct> util = new ExcelUtil<JbProduct>(JbProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @ApiOperation("获取产品详细信息")
    @PreAuthorize("@ss.hasPermi('product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增产品
     */
    @ApiOperation("新增产品")
    @PreAuthorize("@ss.hasPermi('product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbProduct jbProduct)
    {
//        jbProduct.setProductCode(JoCodeUtils.PRODUCT());
        factory.isNotNullByName(jbProduct.getProductName(), null);
        return toAjax(service.save(jbProduct));
    }

    /**
     * 修改产品
     */
    @ApiOperation("修改产品")
    @PreAuthorize("@ss.hasPermi('product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbProduct jbProduct)
    {
        return toAjax(factory.edit(jbProduct));
    }

    /**
     * 修改状态
     */
    @ApiOperation("修改状态")
    @PreAuthorize("@ss.hasPermi('product:isStatus')")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PutMapping("/isStatus")
    public AjaxResult isStatus(@RequestBody JbProduct jbProduct)
    {
        LambdaUpdateWrapper<JbProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JbProduct::getId, jbProduct.getId());
        updateWrapper.set(JbProduct::getStatus, jbProduct.getStatus());

        return toAjax(service.update(updateWrapper));
    }

    /**
     * 删除产品
     */
    @ApiOperation("删除产品")
    @PreAuthorize("@ss.hasPermi('product:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factory.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 新增产品分类
     */
    @ApiOperation("新增产品分类")
    @PreAuthorize("@ss.hasPermi('product:classify:add')")
    @Log(title = "新增产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/addClassify")
    public AjaxResult addClassify(@RequestBody JbProductClassify jbProductClassify)
    {
        jbProductClassify.setType(JbProductClassifyFactory.PRODUCTTYPE);
        return jbProductClassifyFactory.addOrEdit(jbProductClassify);
    }

    /**
     * 删除产品分类
     */
    @ApiOperation("删除产品分类")
    @PreAuthorize("@ss.hasPermi('product:classify:remove')")
    @Log(title = "删除产品分类", businessType = BusinessType.INSERT)
    @DeleteMapping("/removeClassify/{ids}")
    public AjaxResult removeClassify(@PathVariable Long[] ids)
    {
        return jbProductClassifyFactory.remove(ids);
    }

    /**
     * 查询产品分类树
     */
    @ApiOperation("查询产品分类树")
    // @PreAuthorize("@ss.hasPermi('product:classify:list')")
    @Log(title = "查询产品分类树", businessType = BusinessType.INSERT)
    @GetMapping("/listClassify")
    public AjaxResult listClassify()
    {
        return AjaxResult.success(jbProductClassifyFactory.listAll(JbProductClassifyFactory.PRODUCTTYPE));
    }

    /**
     * 模版下载
     *
     * @param response
     */
    @Log(title = "模版下载", businessType = BusinessType.IMPORT)
    @PostMapping("/templateDownload")
    public void templateDownload(HttpServletResponse response)
    {
        List<JbProduct> list = Lists.newArrayList();
        ExcelUtil<JbProduct> util = new ExcelUtil<>(JbProduct.class);
        util.exportExcel(response, list, "产品模版");
    }

    /**
     * 产品导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "产品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<JbProduct> util = new ExcelUtil<>(JbProduct.class);
        List<JbProduct> list = util.importExcel(file.getInputStream());
        boolean b = false;
        if (ObjectUtils.isNotEmpty(list))
        {
            b = factory.importData(list);
        }
        return toAjax(b);
    }

}
